// 订单Mixins

import {
	apiOrderClose,
	apiOrderConfirm,
	apiOrderDelete,
	apiOrderAgain
} from '@/api/order'

const OrderMixin = {
	data() {
		return {
			// 订单按钮组
			ButtonsMap: {
				'payment': {
					event: 'payment',
					name: '立即付款',
					style: 'primary'
				},
				'succeed': {
					event: 'succeed',
					name: '确认收货',
					style: 'normal'
				},
				'evaluate': {
					event: 'evaluate',
					name: '去评价',
					style: 'normal'
				},
				'content': {
					event: 'content',
					name: '查看内容',
					style: 'muted'
				},
				'express': {
					event: 'express',
					name: '查看物流',
					style: 'muted'
				},
				'close': {
					event: 'close',
					name: '取消订单',
					style: 'muted'
				},
				'delete': {
					event: 'delete',
					name: '删除订单',
					style: 'muted'
				},
				'pickup': {
					event: 'pickup',
					name: '查看提货码',
					style: 'normal'
				},
				'again': {
					event: 'again',
					name: '再下一单',
					style: 'normal'
				}
			}
		}
	},

	methods: {
		// 获取订单状态按钮组
		getOrderStatusButtons(buttonStatus) {
			let buttons = []

			// 删除
			buttonStatus.delete_btn && buttons.push(this.ButtonsMap['delete'])
			// 取消
			buttonStatus.cancel_btn && buttons.push(this.ButtonsMap['close'])

			// 查看内容
			buttonStatus.content_btn && buttons.push(this.ButtonsMap['content'])
			// 物流
			buttonStatus.delivery_btn && buttons.push(this.ButtonsMap['express'])
			// 评论
			buttonStatus.comment_btn && buttons.push(this.ButtonsMap['evaluate'])
			// 收货
			buttonStatus.confirm_btn && buttons.push(this.ButtonsMap['succeed'])
			// 提货码
			buttonStatus.pickup_btn && buttons.push(this.ButtonsMap['pickup'])
			// 支付
			buttonStatus.pay_btn && buttons.push(this.ButtonsMap['payment'])
			// 提货码
			buttonStatus.again_btn && buttons.push(this.ButtonsMap['again'])

			return buttons
		},

		// 点击订单按钮映射处理方法
		onOrderButtons(event, orderID, userId, orderExtend, orderGoods) {
			console.log('orderExtend', orderExtend)
			switch (event) {
				case 'payment':
					return this.handlePayment(orderID)
				case 'succeed':
					return this.handleSucceed(orderID)
				case 'evaluate':
					return this.handleEvaluate(orderID, orderGoods)
				case 'express':
					return this.handleExpress(orderID)
				case 'close':
					return this.handleClose(orderID, userId)
				case 'delete':
					return this.handleDelete(orderID)
				case 'pickup':
					return this.handlePickup(orderID)
				case 'content':
					return this.handleContent(orderID)
				case 'again':
					return this.handleAgain(orderID, orderExtend)
			}
		},

		// 刷新订单数据
		refreshOrderData() {
			throw new Error(this.$zhTran('未初始化刷新方法'))
		},

		// 处理：支付
		handlePayment(orderID) {
			this.$Router.push({
				path: `/bundle2/pages/payment/payment`,
				query: {
					from: 'order',
					order_id: orderID
				}
			})
		},

		// 处理：确认收货
		handleSucceed(orderID) {
			uni.showModal({
				title: this.$zhTran('温馨提示'),
				content: this.$zhTran('是否确认收货?'),
				confirmColor: this.themeColor,
				success: ({
					confirm
				}) => {
					if (!confirm) return
					// uni.showLoading()
					// 取消订单
					apiOrderConfirm({
						id: orderID
					}).then(data => {
						this.$toast({
							title: this.$zhTran('收货成功')
						})
						this.refreshOrderData()
					}).catch(err => {
						console.log(err)
					}).finally(() => {
						// uni.hideLoading()
					})
				}
			});
		},
		handleContent(orderID) {
			this.$Router.push({
				path: `/bundle2/pages/order_detail/order_detail`,
				query: {
					order_id: orderID
				}
			})
		},
		// 处理：去评价
		handleEvaluate(orderID, orderGoods) {
			console.log('orderGoods', orderGoods)
			if (orderGoods.length > 1) {
				this.$Router.push({
					path: '/bundle/pages/goods_comment/goods_comment',
					query: {
						order_id: orderID
					}
				})
			} else {
				this.$Router.push({
					path: '/bundle/pages/post_comment/post_comment',
					query: {
						id: orderGoods[0].id
					}
				})
			}
		},

		// 处理：查看物流
		handleExpress(orderID) {
			this.$Router.push({
				path: '/bundle/pages/order_logistics/order_logistics',
				query: {
					order_id: orderID
				}
			})
		},

		// 处理：关闭订单
		handleClose(orderID, userId) {
			uni.showModal({
				title: this.$zhTran('温馨提示'),
				content: this.$zhTran('是否取消订单?'),
				confirmColor: this.themeColor,
				success: ({
					confirm
				}) => {
					if (!confirm) return
					// uni.showLoading()
					// 取消订单
					let options = {
						id: orderID,
					}
					if (userId) options.user_id = userId

					apiOrderClose(options).then(data => {
						this.$toast({
							title: this.$zhTran('取消成功')
						})
						this.refreshOrderData()
					}).catch(err => {
						console.log(err)
					}).finally(() => {
						// uni.hideLoading()
					})
				}
			})
		},

		// 处理：删除订单
		handleDelete(orderID) {
			uni.showModal({
				title: this.$zhTran('温馨提示'),
				content: this.$zhTran('是否删除订单?'),
				confirmColor: this.themeColor,
				success: ({
					confirm
				}) => {
					if (!confirm) return
					// uni.showLoading()
					// 取消订单
					apiOrderDelete({
						id: orderID
					}).then(data => {
						this.$toast({
							title: this.$zhTran('删除成功')
						})
						let pagesArr = getCurrentPages()
						let currentPage = pagesArr.at(-1).route
						if (currentPage == 'bundle2/pages/order_detail/order_detail') {
							this.$Router.replace({
								path: `/bundle2/pages/order_list/order_list`,
							})
						} else {
							this.refreshOrderData()
						}
					}).catch(err => {
						console.log(err)
					}).finally(() => {
						// uni.hideLoading()
					})
				}
			})
		},

		// 处理：查看提货码
		handlePickup(orderID) {
			this.$Router.push({
				path: `/bundle2/pages/order_detail/order_detail`,
				query: {
					order_id: orderID
				}
			})
		},

		// 再下一单
		handleAgain(orderID, orderExtend) {
			let place_order_again = orderExtend.place_order_again || 0
			if (!place_order_again) {
				this.$toast({
					title: this.$zhTran('下单失败')
				})
				return
			}
			if (orderExtend.place_order_again == 2) {
				//购物车，清空原有商品
				uni.showModal({
					title: this.$zhTran('温馨提示'),
					content: this.$zhTran('是否再下一单?该操作将清空购物车现有商品，把订单商品加入购物车'),
					confirmColor: this.themeColor,
					success: ({
						confirm
					}) => {
						if (!confirm) return
						// uni.showLoading()
						apiOrderAgain({
							id: orderID
						}).then(data => {
							this.$toast({
								title: this.$zhTran('加入成功')
							})
							this.$Router.push({
								path: `/bundle2/pages/shop_cart/shop_cart`,
							})
						}).catch(err => {
							console.log(err)
						}).finally(() => {
							// uni.hideLoading()
						})
					}
				})
			} else {
				var that = this
				uni.navigateTo({
					url: "/bundle2/pages/goods_order/goods_order",
					events: {
						reCountCarNum: function(o) {
							that.$store.dispatch("getCartNum");
						}
					},
					success(res) {
						res.eventChannel.emit("receiveData", {
							from: 0,
							payload: {
								source: "buy_again",
								again_order: orderID
							},
						});
					}
				});
			}
		},
	}
}

export default OrderMixin